﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace UML_Creator.Model
{
    class Storage
    {
        private static void save(string fileName, XmlWrapper wrapper)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(XmlWrapper));

            TextWriter textWriter = new StreamWriter(fileName);

            serializer.Serialize(textWriter, wrapper);
            textWriter.Close();
        }

        public static void AsyncSave(string fName, ObservableCollection<Edge> edges, ObservableCollection<Diagram> diagrams)
        {
            Action<object> saveAction = (object blah) =>
            {
                XmlWrapper wrapper = new XmlWrapper(edges, diagrams);

                save(fName, wrapper);
            };

            Task t1 = new Task(saveAction, "blargh");

            t1.Start();
        }

        private static XmlWrapper load(string fileName)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(XmlWrapper));

            TextReader textReader = new StreamReader(fileName);
            XmlWrapper wrapper;
            try
            {
                wrapper = (XmlWrapper)deserializer.Deserialize(textReader);
            }
            catch (Exception)
            {
                MessageBox.Show("The file was either of a wrong format or corrupted.", "Error reading file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }


            Debug.WriteLine(wrapper.Diagrams.Count);
            Debug.WriteLine(wrapper.Edges.Count);

            return wrapper;
        }

        public static XmlWrapper AsyncLoad(string fName)
        {
            /*XmlWrapper wrapper;
            Action<object> loadAction = (object blah) =>
            {
                wrapper = load(fName);

                
                wrapper.Edges;
                diagrams = wrapper.Diagrams;
            };

            Task t1 = new Task(loadAction, "blargh");
            t1.Start();*/

            Task<XmlWrapper> task = new Task<XmlWrapper>(() => 
            {
                return load(fName);
            });

            task.Start();

            if (task.Result != null)
            {

                Debug.WriteLine("Edges: " + task.Result.Edges.Count);

                return task.Result;
            }
            return null;
        }
    }

    [Serializable]
    public class XmlWrapper
    {
        public ObservableCollection<Edge> Edges { get; set; }
        public ObservableCollection<Diagram> Diagrams { get; set; }

        public XmlWrapper(ObservableCollection<Edge> edges, ObservableCollection<Diagram> diagrams)
        {
            Edges = edges;
            Diagrams = diagrams;
        }

        public XmlWrapper()
        {

        }
    }
}
